/*************************************************
 *FileName:      addsite.js
 *Description:   新增收货地址.js
 *Others:        陈灿
 *************************************************/
    /*
     * 启动
     */
    mui.init();
    mui('.mui-scroll-wrapper').scroll({
        indicators: false,      //是否显示滚动条 默认为true
        deceleration: 0.003,    //阻尼系数,系数越小滑动越灵敏 默认0.0006
        bounce: false           //是否启用回弹 默认true
    });
    
    /*
     * 变量初始化
     */
    var that=getUrlParam('that');
    var uid = localStorage.replace_uid;
    var token = localStorage.replace_token;
    var province , city ,district ;
    /*
     * 主程序
     */ 
    $(document).ready(function() 
    {
        //样式加载
        pagecss();
    });
    /**
     * pagecss
     * 样式加载
     * @cc 
     * @DateTime 2018-01-08T06:19:16+0800
     * @param    int        
     * @return   void
     */
    function pagecss(){
        if(that){
            $('.mui-title').text('修改收货地址');
            getlist(that);
        }
        
        var _getParam = function(obj, param) {return obj[param] || '';};
        var cityPicker3 = new mui.PopPicker({layer: 3});
        cityPicker3.setData(cityData3);
        $('.ads').on('tap', function(e) {
            $(this).blur();
            cityPicker3.show(function(items) {
                province=_getParam(items[0], 'text');
                city=_getParam(items[1], 'text');
                district=_getParam(items[2], 'text');
                $('.ads').val(province+'-'+city+'-'+district);
            });
        })
        
        $('.sex').on('tap', function(e) {
            $(this).addClass('active').siblings().removeClass('active');
        })
    }

//提交
$('.rdntn').on('tap',function(){
    var name =$('.name').val();
    var sex =$('.active').index()==1 ? 'man': 'woman';
    var ophone =$('.phone').val();
    var ads =$('.ads').val();
    var address =$('.adds').val();
    if(!name){
        mui.toast('请输入姓名！');
        return false; 
    }else if(!(/^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(ophone) ) ){ //判断手机号
        mui.toast('请输入正确手机号码！');
        return false; 
    }else if(!ads){
        mui.toast('请选择地址！');
        return false; 
    }else if(!address){
        mui.toast('请输入详细地址！');
        return false; 
    }else{
        if(that){
            var ourl ="/index.php/api/user/addressEdit";
            var arr={ uid: uid, token: token,sex:sex,province:province,city:city,district:district,address:address,phone:ophone,name:name,id:that};
            uplode(ourl,arr);
        }else{
            var ourl ="/index.php/api/user/addressAdd";
            var arr={ uid: uid, token: token,sex:sex,province:province,city:city,district:district,address:address,phone:ophone,name:name};
            uplode(ourl,arr);
        }
    }
})

//获取信息
function getlist(oid){
    $.ajax({
        type: "post",
        url: DOMAIN_NAME + "/index.php/api/user/getOneAddress",
        dataType:'jsonp',
        data: { uid: uid, token: token,id:oid},
        success: function(data) {
            if(data.error_code == 0){
                $('.sex').removeClass('active');
                data.data.sex=='man' ? $('.sex').eq(0).addClass('active'): $('.sex').eq(1).addClass('active');
                $('.name').val(data.data.name);
                $('.phone').val(data.data.phone);
                $('.ads').val(data.data.province+'-'+data.data.city+'-'+data.data.district);
                $('.adds').val(data.data.address);
                province=data.data.province;
                city=data.data.city;
                district=data.data.district;
            }else if(data.error_code == 1001){
                setTimeout(function(){
                    exitlogin();
                },1000);
            }else{
                mui.toast(data.error_msg)
            }
        }  
    });
}

//提交
function uplode(ourl,arr){
    mui.confirm('确认提交？','提示',['确定','取消'],function(e){
        if(e.index==0){
            $.ajax({
                type: "post",
                url: DOMAIN_NAME + ourl,
                dataType:'jsonp',
                data: arr,
                success: function(data) {
                    if(data.error_code == 0){
                        mui.toast('提交成功！');
                        setTimeout( mui.back(),350);
                        var list = plus.webview.currentWebview().opener();
                        mui.fire(list, 'refreshsite');//返回true,继续页面关闭逻辑
                        return true;
                    }else if(data.error_code == 1001){
                        setTimeout(function(){
                            exitlogin();
                        },1000);
                    }else{
                    }
                }  
            });
        }
    },'div')
}